package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.commons.exception.LateException;
import com.woniuxy.commons.utile.BeanCopyUtil;
import com.woniuxy.commons.utile.Result;
import com.woniuxy.controller.dto.MyonerepairDto;
import com.woniuxy.controller.dto.PageRepairInfoDto;
import com.woniuxy.controller.dto.myrepairDto;
import com.woniuxy.controller.form.*;
import com.woniuxy.dao.model.RepairEvaluate;
import com.woniuxy.dao.model.RepairInfo;
import com.woniuxy.service.RepairEvaluateService;
import com.woniuxy.service.RepairInfoService;
import com.woniuxy.service.RepairLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Console;


/**
 * <p>
 * 维修表 前端控制器
 * </p>
 *
 * @author Keep it simple
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/repairInfo")
@Api(tags = "维修信息大类接口")
public class RepairInfoController {

    @Resource
    private RepairInfoService repairInfoService;

    @Resource
    private RepairEvaluateService repairEvaluateService;

    @Resource
    RepairLogService repairLogService;

    //添加维修信息的方法
    @ApiOperation("添加一条维修信息")
    @PostMapping("/addrepair")
    @ResponseBody
    public Result addRepair(@Valid @RequestBody RepairuserForm repairuserForm){
        System.out.println(repairuserForm);
        Integer integer = repairInfoService.addRepariInfo(repairuserForm);
        if (integer>0){
            return Result.instance().setCode(200).setMessage("成功").setData(null);
        }else {
            throw new LateException(1001,"保修申请错误");
        }
    }

    @ApiOperation("查询维修信息（条件可选）")
    @PostMapping("/showRepair")
    @ResponseBody
    public Result showRepair(@Valid @RequestBody RepairInfoForm repairInfoForm){
        System.out.println(repairInfoForm);
        PageRepairInfoForm pageRepairInfoForm = new PageRepairInfoForm();
        pageRepairInfoForm = BeanCopyUtil.copyOne(repairInfoForm, PageRepairInfoForm.class);
        System.out.println(pageRepairInfoForm+"**************");
        PageRepairInfoDto repairInfo = repairInfoService.getRepairInfo(pageRepairInfoForm);
        return Result.instance().setData(repairInfo);
    }

    @ApiOperation("接受请求")
    @PostMapping("/acceptRepair")
    @ResponseBody
    public Result acceptRepair(@Valid @RequestBody AcceptRepairInfoForm acceptRepairInfoForm){
        System.out.println(acceptRepairInfoForm);
        UpdateRepairInfoForm updateRepairInfoForm = BeanCopyUtil.copyOne(acceptRepairInfoForm,UpdateRepairInfoForm.class);
        System.out.println(updateRepairInfoForm);
        Result result = repairLogService.updateRepairInfo(updateRepairInfoForm);
        return result;
    }

    @ApiOperation("驳回请求")
    @PostMapping("/rejectRepair")
    @ResponseBody
    public Result rejectRepair(@Valid @RequestBody RejectForm rejectForm){
        UpdateRepairInfoForm updateRepairInfoForm = BeanCopyUtil.copyOne(rejectForm,UpdateRepairInfoForm.class);
        Result result = repairLogService.updateRepairInfo(updateRepairInfoForm);
        return result;
    }

    @ApiOperation("分配维修工")
    @PostMapping("/allocateRepair")
    @ResponseBody
    public Result allocateRepair(@Valid @RequestBody AllocateRepairForm allocateRepairForm){
        UpdateRepairInfoForm updateRepairInfoForm = BeanCopyUtil.copyOne(allocateRepairForm,UpdateRepairInfoForm.class);
        RepairInfo updateModel = new RepairInfo();
        updateModel.setRepairnum(allocateRepairForm.getRepairnum());
        updateModel.setRepairpersonid(allocateRepairForm.getRepairpersonid());
        repairInfoService.update(updateModel,new UpdateWrapper<RepairInfo>().eq("repairNum",updateModel.getRepairnum()));
        Result result = repairLogService.updateRepairInfo(updateRepairInfoForm);
        return result;
    }

    @ApiOperation("维修工接单")
    @PostMapping("/repairerAccept")
    @ResponseBody
    public Result repairerAccept(@Valid @RequestBody RepairerAcceptForm repairerAcceptForm){
        UpdateRepairInfoForm updateRepairInfoForm = BeanCopyUtil.copyOne(repairerAcceptForm,UpdateRepairInfoForm.class);
        Result result = repairLogService.updateRepairInfo(updateRepairInfoForm);
        return result;
    }


//    @ApiOperation("管理员查询所有的维修信息")
//    @PostMapping("/showmyrepair")
//    @ResponseBody
//    @ApiImplicitParams({@ApiImplicitParam(name = "studentForm.studentid",value = "登陆账号的id"),
//            @ApiImplicitParam(name = "studentForm.page",value = "当前页数")})
//    public Result showmyrepair(@RequestBody studentForm studentForm){
//        myrepairDto personaluser = repairInfoService.personaluser(studentForm);
//        System.out.println(personaluser);
//        return Result.instance().setCode(200).setMessage("成功").setData(personaluser);
//    }
//
//
//
    @ApiOperation("展示我的维修详细信息")
    @PostMapping("/showpersnal")
    @ResponseBody
    public Result showrepair(@Valid @RequestBody ShowrepairForm showrepairForm){
        System.out.println(showrepairForm);
        MyonerepairDto showrepair = repairInfoService.showrepair(showrepairForm);
        RepairEvaluate isexist = repairEvaluateService.isexist(showrepairForm.getRepairNum());
        if (isexist!=null){
            showrepair.setTimelystar(isexist.getTimelystar());
            showrepair.setAttitudestar(isexist.getAttitudestar());
            showrepair.setEvaluatecontent(isexist.getEvaluatecontent());
        }

        System.out.println(showrepair);
        return Result.instance().setCode(200).setMessage("成功").setData(showrepair);
    }
//
//    /**
//     * 获取维修工人维修的所有信息
//     * @param
//     * @return
//     */
    @GetMapping("/showWorkerRepair")
    @ResponseBody
    @ApiOperation("获取当前帐号的所有维修信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum",value = "页数"),
                        @ApiImplicitParam(name = "pageSize",value = "条数")})
    public Result showWorkerRepair(Integer pageNum,Integer pageSize){
        Page<RepairForm> page = repairInfoService.showWorkerRepair(pageNum,pageSize);
        return Result.instance().setCode(200).setMessage("成功").setData(page);
    }
//
//    @GetMapping("/getRepairInfo")
//    @ResponseBody
//    @ApiOperation("获取维修信息")
////    @ApiImplicitParams({
////            @ApiImplicitParam(name = "selectArea",value = "区域id"),
////            @ApiImplicitParam(name = "selectType",value = "维修类型id")
////    })
//    public Result getRepair(@RequestBody PageRepairInfoForm pageRepairInfoForm){
//        PageRepairInfoDto repairInfo = repairInfoService.getRepairInfo(pageRepairInfoForm);
//        return Result.instance().setData(repairInfo);
//    }


}

